package com.entrogy.controller;

import com.entrogy.bean.BusLineEntity;
import com.entrogy.bean.Msg;
import com.entrogy.service.*;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName SchoolBusLineController
 * @Description
 * @Author luoxingjiang
 * @Date 2019-11-07 下午 2:03
 * @Version 1.0
 **/
@RestController
@RequestMapping("/bus-line")
@Api(description = "校车路线相关接口")
public class BusLineController {

    private Logger logger = LoggerFactory.getLogger(BusLineController.class);

    @Autowired
    private BusLineService busLineService;

    @Autowired
    private UserService userService;

    @Autowired
    private AttendanceService attendanceService;

    @GetMapping(value = "/list")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "uid", value = "用户id", dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "isLimit", value = "是否需要分页，0不需要，1需要", dataType = "Integer", paramType = "query"),
        @ApiImplicitParam(name = "currentPage", value = "当前页", dataType = "Integer", paramType = "query"),
        @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "Integer", paramType = "query"),
        @ApiImplicitParam(name = "selectSchoolCode", value = "运维管理员查询校车路线时，选择查看哪个学校的校车路线", dataType = "Integer", paramType = "query"),
    })
    @ApiOperation(value = "查看校车路线")
    public Msg listSalarySlipTemplate(String uid, Integer isLimit, Integer currentPage, Integer pageSize, Integer selectSchoolCode){
        Integer schoolCode = selectSchoolCode == null ? (Integer) SecurityUtils.getSubject().getSession().getAttribute("schoolCode") : selectSchoolCode;
        // 如果没有登录，那么就是小程序端的请求
        if (schoolCode == null){
            schoolCode = userService.getSchoolCodeByUserId(uid);
        }
        List<BusLineEntity> schoolBusLineList = new ArrayList<>();
        Integer totalCount = 0;
        if (isLimit == null || isLimit == 0){
            schoolBusLineList = busLineService.listSchoolBusLine(schoolCode);
        }else if (isLimit == 1){
            totalCount = busLineService.countSchoolBusLine(schoolCode);
            PageHelper.startPage(currentPage, pageSize);
            schoolBusLineList = busLineService.listSchoolBusLine(schoolCode);
        }
        return Msg.success().add("schoolBusLineList", schoolBusLineList).add("totalCount", totalCount);
    }

    @PutMapping(value = "/insert")
    @ApiOperation(value = "增加校车路线")
    public Msg insertBusLine(@RequestBody BusLineEntity busLine){
        String userId = (String) SecurityUtils.getSubject().getSession().getAttribute("userId");
        Integer schoolCode = (Integer) SecurityUtils.getSubject().getSession().getAttribute("schoolCode");
        busLine.setCreateUserId(userId).setSchoolCode(schoolCode).setOperateUserId(userId);
        boolean result = busLineService.insertBusLine(busLine);
        return Msg.success();
    }

    @DeleteMapping(value = "/delete")
    @ApiOperation(value = "删除校车路线")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "busLineId", value = "校车路线id", dataType = "Integer", paramType = "query"),
    })
    public Msg deleteBusLine(Integer busLineId){
        String userId = (String) SecurityUtils.getSubject().getSession().getAttribute("userId");
        boolean result = busLineService.deleteBusLine(busLineId, userId);
        return Msg.success();
    }

    @PutMapping(value = "/update")
    @ApiOperation(value = "更新校车路线")
    public Msg deleteBusLine(@RequestBody BusLineEntity busLine){
        String userId = (String) SecurityUtils.getSubject().getSession().getAttribute("userId");
        busLine.setOperateUserId(userId);
        boolean result = busLineService.updateBusLine(busLine);
        return Msg.success();
    }

    @PostMapping(value = "/send-message")
    @ApiOperation(value = "发送上车消息给学生父母")
    public Msg sendMessage(@RequestParam(value = "studentIdList") List<Integer> studentIdList, @RequestParam(value = "type") Integer type, @RequestParam(value = "uid") String uid){
        logger.info("正在进行校车考勤消息发送，学生id=>" + studentIdList);
        boolean reslult = busLineService.sendMessage(studentIdList, type, uid);
        return Msg.success();
    }

    @GetMapping(value = "/get-day-type")
    @ApiOperation(value = "得到今天是离校日还是返校日，同一天，可能有些年级是离校日，有些学校是返校日")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "uid", value = "用户id", dataType = "String", paramType = "query"),
    })
    public Msg getDayType(String uid){
        Integer schoolCode = userService.getSchoolCodeByUserId(uid);
        // 得到当前日期
        String currentDate = new SimpleDateFormat("yyyyMMdd").format(new Date());
        String tableName = "dyn_arrange" + currentDate;
        List<Integer> dayTypeList = attendanceService.listAllDayType(tableName, schoolCode);
        Integer dayType = 0;
        if (dayTypeList.contains(1) && dayTypeList.contains(3)){
            // -1表示两个类型都有
            dayType = -1;
        }else if (dayTypeList.contains(1)){
            // 返校日
            dayType = 1;
        }else if (dayTypeList.contains(3)){
            // 离校日
            dayType = 3;
        }
        return Msg.success().add("dayType", dayType);
    }





}
